package zhr.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import zhr.store.Dto.SetmealDto;
import zhr.store.common.Result;
import zhr.store.domain.Category;
import zhr.store.domain.Setmeal;
import zhr.store.service.CategoryService;
import zhr.store.service.SetmealService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 分页查询套餐信息
     */
    @GetMapping("/page")
    public Result<Page> selectAll(int page, int pageSize, String name) {
        //构建分页插件
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        if (name != null) {
            setmealLambdaQueryWrapper.like(Setmeal::getName, name);
        }
        //构建排序条件
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, setmealLambdaQueryWrapper);
        //拷贝数据
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        //套餐分类赋值
        ArrayList<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝数据
            BeanUtils.copyProperties(record, setmealDto);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            if (categoryName == null) {
                break;
            }
            setmealDto.setCategoryName(categoryName);
            list.add(setmealDto);
        }
        setmealDtoPage.setRecords(list);
        return Result.success(setmealDtoPage);
    }
    /**
     * 新增套餐
     */
    @CacheEvict(value ="SetmealCache",allEntries = true)
    @PostMapping
    public Result<String> insertSetMeal(@RequestBody SetmealDto setmealDto) {
        setmealService.insertSetMealAndDish(setmealDto);
        return Result.success("新增套餐成功");
    }
    /**
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> selectById(@PathVariable Long id) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getId, id);
        SetmealDto setmealDto = setmealService.selectById(id);
        return Result.success(setmealDto);
    }

    /**
     * 修改套餐
     */
    @CacheEvict(value ="SetmealCache",allEntries = true)
    @PutMapping
    public Result<String> updateSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.updateSetmeal(setmealDto);
        return Result.success("修改成功");
    }

    /**
     * 删除套餐
     */
    @CacheEvict(value ="SetmealCache",allEntries = true)
    @DeleteMapping
    public Result<String> deleteSetmeal(@RequestParam List<Long> ids) {
        setmealService.deleteSetmeal(ids);
        return Result.success("删除成功");
    }

    /**
     * 根据id查询套餐
     */
    @Cacheable(value = "SetmealCache", key = "#setmeal.categoryId", unless = "#result==null")
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        if (setmeal.getCategoryId() != null) {
            setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
            setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
            //构建排序条件
            setmealLambdaQueryWrapper.orderByAsc(Setmeal::getPrice)
                    .orderByDesc(Setmeal::getUpdateTime);
        }
        //执行查询
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return Result.success(list);
    }

    /**
     * 启售
     */
    @CacheEvict(value ="SetmealCache",allEntries = true)
    @PostMapping("/status/1")
    public Result<String> open(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            Integer status = setmeal.getStatus();
            //判断
            if (status == 1) {
                return Result.error("套餐已启售");
            }
            //启售
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        }
        return Result.success("启售成功");
    }

    /**
     * 停售
     */
    @CacheEvict(value ="SetmealCache",allEntries = true)
    @PostMapping("/status/0")
    public Result<String> ban(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            Integer status = setmeal.getStatus();
            //判断
            if (status == 0) {
                return Result.error("套餐已停售");
            }
            //停售
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        }
        return Result.success("停售成功");
    }
}
